package directive;

import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import modele.beans.Employee;
import modele.beans.EmployeeKey;
import modele.beans.Key;
import modele.dao.EmployeeDAO;
import modele.dao.EmployeeKeyDAO;
import modele.dao.KeyDAO;
import modele.form.EmployeeKeyForm;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

public class EmployeeKeyAction extends GlobalAction {
	private KeyDAO keyDAO;
	private EmployeeDAO empDAO;
	private EmployeeKeyDAO empKeyDAO;
	
	@Override
	public String executeAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		keyDAO = new KeyDAO(this.conn);
		empDAO = new EmployeeDAO(this.conn);
		empKeyDAO = new EmployeeKeyDAO(this.conn);
		
		String type = (String) request.getParameter("type");
		String ac = ACTION_FAIL;

		if (type.equals("Ajouter"))
			ac = AddEmployeeKey(mapping, form);
		else if (type.equals("Supprimer"))
			ac = DeleteEmployeeKey(mapping, form);
		else
			ac = ACTION_SUCCESS;
		
		EmployeeKeyForm employeeKey = (EmployeeKeyForm) form;
		
		if (ac.equals(ACTION_SUCCESS)) {
			employeeKey.setEmployeeID("");;
			employeeKey.setKeyID("");
		}
	
		employeeKey.setEmployeeKeyList(GetEmployeeKeyList());
		employeeKey.setEmployees(GetEmployees());
		employeeKey.setKeys(GetKeys());
		request.setAttribute("EmployeeKeyForm", employeeKey);
		
		return ac;
	}

	private String AddEmployeeKey(ActionMapping mapping, ActionForm form) {
		EmployeeKeyForm empKey = (EmployeeKeyForm) form;

		EmployeeKey ek = new EmployeeKey(new Key("", "", empKey.getKeyID()),
					new Employee("", "", "", "", empKey.getEmployeeID()));
		try {
			empKeyDAO.AddEmployeeKey(ek);
		} catch (SQLException e) {
			return ACTION_FAIL;
		} catch (ClassNotFoundException e) {
			return ACTION_FAIL;
		}
		return ACTION_SUCCESS;
	}
	
	private String DeleteEmployeeKey(ActionMapping mapping, ActionForm form) {
		EmployeeKeyForm empKey = (EmployeeKeyForm) form;

		EmployeeKey ek = new EmployeeKey(new Key("", "", empKey.getKeyID()),
					new Employee("", "", "", "", empKey.getEmployeeID()));
		try {
			empKeyDAO.RemoveEmployeeKey(ek);
		} catch (SQLException e) {
			return ACTION_FAIL;
		} catch (ClassNotFoundException e) {
			return ACTION_FAIL;
		}
		return ACTION_SUCCESS;
	}
	
	private ArrayList<EmployeeKey> GetEmployeeKeyList() {
		try {
			return empKeyDAO.GetEmployeeKeys();
		} catch (SQLException e) {
			return new ArrayList<EmployeeKey>();
		} catch (ClassNotFoundException e) {
			return new ArrayList<EmployeeKey>();
		}
	}

	private ArrayList<Key> GetKeys() {
		try {
			return keyDAO.GetKeys();
		} catch (SQLException e) {
			return new ArrayList<Key>();
		} catch (ClassNotFoundException e) {
			return new ArrayList<Key>();
		}
	}
	
	private ArrayList<Employee> GetEmployees() {
		try {
			return empDAO.GetEmployees();
		} catch (SQLException e) {
			return new ArrayList<Employee>();
		} catch (ClassNotFoundException e) {
			return new ArrayList<Employee>();
		}
	}
}